package com.fr.cell.core;

import com.fr.base.BaseUtils;
import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.Nameable;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.dav.Env;
import com.fr.base.dav.LocalEnv;
import com.fr.base.file.DatasourceManager;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.report.TemplateWorkBook;
import com.fr.report.cellElement.core.DSColumn;
import com.fr.util.Consts;
import com.fr.util.OperatingSystem;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractButton;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSpinner;
import javax.swing.JSpinner.DefaultEditor;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.TableModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class GUICoreUtils
{
  public static void setSelectedItem(JComboBox paramJComboBox, Object paramObject)
  {
    paramJComboBox.setSelectedItem(paramObject);
    if (paramJComboBox.getSelectedItem() != paramObject)
      paramJComboBox.getModel().setSelectedItem(paramObject);
  }

  public static TitledBorder createTitledBorder(String paramString, Color paramColor)
  {
    TitledBorder localTitledBorder = new TitledBorder(paramString);
    if (paramColor == null)
      paramColor = new Color(102, 153, 255);
    localTitledBorder.setTitleColor(paramColor);
    return localTitledBorder;
  }

  public static JComponent createToolBarPane(JComponent paramJComponent, boolean paramBoolean, LayoutManager paramLayoutManager)
  {
    JToolBar localJToolBar = new JToolBar();
    localJToolBar.setBackground(Color.darkGray);
    localJToolBar.setLayout((paramLayoutManager == null) ? new BorderLayout() : paramLayoutManager);
    localJToolBar.setFloatable(paramBoolean);
    localJToolBar.add(paramJComponent);
    return localJToolBar;
  }

  public static void setWindowCenter(Window paramWindow1, Window paramWindow2)
  {
    Point localPoint = paramWindow1.getLocation();
    Dimension localDimension1 = paramWindow1.getSize();
    Dimension localDimension2 = paramWindow2.getSize();
    paramWindow2.setLocation((localDimension1.width - localDimension2.width) / 2 + localPoint.x, (localDimension1.height - localDimension2.height) / 2 + localPoint.y);
  }

  public static void setWindowFullScreen(Window paramWindow)
  {
    Dimension localDimension = Toolkit.getDefaultToolkit().getScreenSize();
    if (OperatingSystem.isWindows())
    {
      paramWindow.setLocation(0, 0);
      paramWindow.setSize(localDimension.width, localDimension.height - 28);
    }
    else
    {
      paramWindow.setLocation(5, 23);
      paramWindow.setSize(localDimension.width, localDimension.height - 56);
    }
  }

  public static void showPopupCloseMenu(JPopupMenu paramJPopupMenu, Component paramComponent)
  {
    if (paramJPopupMenu == null)
      return;
    Dimension localDimension = Toolkit.getDefaultToolkit().getScreenSize();
    Window localWindow = SwingUtilities.getWindowAncestor(paramComponent);
    int i = 0;
    int j = paramComponent.getLocation().x + localWindow.getLocation().x + paramJPopupMenu.getPreferredSize().width;
    if (j > localDimension.width)
      i = localDimension.width - j;
    paramJPopupMenu.show(paramComponent, i, paramComponent.getSize().height);
  }

  public static void showPopupMenu(JPopupMenu paramJPopupMenu, Component paramComponent, int paramInt1, int paramInt2)
  {
    if (paramJPopupMenu == null)
      return;
    Point localPoint = new Point(paramInt1, paramInt2);
    SwingUtilities.convertPointToScreen(localPoint, paramComponent);
    Dimension localDimension1 = paramJPopupMenu.getPreferredSize();
    Dimension localDimension2 = Toolkit.getDefaultToolkit().getScreenSize();
    localDimension2.setSize(localDimension2.getSize().width, localDimension2.height - 28);
    if ((localPoint.x + localDimension1.width > localDimension2.width) && (localDimension1.width < localDimension2.width))
      paramInt1 += localDimension2.width - localPoint.x - localDimension1.width;
    if ((localPoint.y + localDimension1.height > localDimension2.height) && (localDimension1.height < localDimension2.height))
      paramInt2 -= localDimension1.height;
    paramJPopupMenu.show(paramComponent, paramInt1, paramInt2);
  }

  public static void setEnabled(JComponent paramJComponent, boolean paramBoolean)
  {
    Border localBorder = paramJComponent.getBorder();
    if ((localBorder != null) && (localBorder instanceof TitledBorder))
    {
      TitledBorder localTitledBorder = (TitledBorder)localBorder;
      if (paramBoolean)
        localTitledBorder.setTitleColor(UIManager.getColor("Label.foreground"));
      else
        localTitledBorder.setTitleColor(UIManager.getColor("Label.disabledForeground"));
    }
    for (int i = 0; i < paramJComponent.getComponentCount(); ++i)
    {
      Component localComponent = paramJComponent.getComponent(i);
      if (localComponent instanceof JComponent)
        setEnabled((JComponent)localComponent, paramBoolean);
      else
        localComponent.setEnabled(paramBoolean);
    }
    paramJComponent.setEnabled(paramBoolean);
  }

  public static Applet getAppletAncestor(Component paramComponent)
  {
    for (Container localContainer = paramComponent.getParent(); localContainer != null; localContainer = localContainer.getParent())
      if (localContainer instanceof Applet)
        return ((Applet)localContainer);
    return null;
  }

  public static JPanel createNamedPane(JComponent paramJComponent, String paramString)
  {
    JPanel localJPanel = new JPanel();
    localJPanel.setLayout(new BorderLayout(4, 4));
    localJPanel.add(new JLabel(paramString), "West");
    localJPanel.add(paramJComponent, "Center");
    return localJPanel;
  }

  public static JPanel createVerticalNamedPane(JComponent paramJComponent, String paramString)
  {
    JPanel localJPanel = new JPanel();
    localJPanel.setLayout(new BorderLayout(4, 4));
    localJPanel.add(new JLabel(paramString), "North");
    localJPanel.add(paramJComponent, "Center");
    return localJPanel;
  }

  public static JPanel createFlowPane(Component paramComponent, int paramInt)
  {
    return createFlowPane(new Component[] { paramComponent }, paramInt);
  }

  public static JPanel createFlowPane(Component[] paramArrayOfComponent, int paramInt)
  {
    return createFlowPane(paramArrayOfComponent, paramInt, 0);
  }

  public static JPanel createFlowPane(Component[] paramArrayOfComponent, int paramInt1, int paramInt2)
  {
    JPanel localJPanel = new JPanel();
    localJPanel.setLayout(new FlowLayout(paramInt1, paramInt2, 0));
    for (int i = 0; i < paramArrayOfComponent.length; ++i)
      localJPanel.add(paramArrayOfComponent[i]);
    return localJPanel;
  }

  public static JPanel createBorderPane(JComponent paramJComponent, String paramString)
  {
    JPanel localJPanel = new JPanel();
    localJPanel.setLayout(new BorderLayout());
    localJPanel.add(paramJComponent, paramString);
    return localJPanel;
  }

  public static String[] getSelectedColumnNames(TemplateWorkBook paramTemplateWorkBook, DSColumn paramDSColumn)
  {
    TableData localTableData = (paramTemplateWorkBook == null) ? null : paramTemplateWorkBook.getTableData(paramDSColumn.getDSName());
    if (localTableData == null)
    {
      localObject = FRContext.getDatasourceManager();
      localTableData = ((DatasourceManager)localObject).getTableData(paramDSColumn.getDSName());
    }
    if (localTableData == null)
      return new String[0];
    Object localObject = null;
    if (FRContext.getCurrentEnv() instanceof LocalEnv)
      localObject = localTableData;
    else
      try
      {
        localObject = FRContext.getCurrentEnv().previewTableData(localTableData, Collections.EMPTY_MAP, 0);
      }
      catch (Exception localException)
      {
        FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      }
    if (localObject != null)
    {
      int i;
      ArrayList localArrayList = new ArrayList();
      try
      {
        for (i = 0; i < ((TableData)localObject).getColumnCount(); ++i)
          localArrayList.add(((TableData)localObject).getColumnName(i));
      }
      catch (TableDataException localTableDataException)
      {
        FRContext.getLogger().log(Level.WARNING, localTableDataException.getMessage(), localTableDataException);
      }
      String[] arrayOfString = new String[localArrayList.size()];
      localArrayList.toArray(arrayOfString);
      return arrayOfString;
    }
    return ((String)new String[0]);
  }

  public static TreePath getTreePath(TreeNode paramTreeNode)
  {
    ArrayList localArrayList = new ArrayList();
    if (paramTreeNode != null)
    {
      localArrayList.add(paramTreeNode);
      while ((paramTreeNode = paramTreeNode.getParent()) != null)
        localArrayList.add(0, paramTreeNode);
    }
    Object[] arrayOfObject = new Object[localArrayList.size()];
    localArrayList.toArray(arrayOfObject);
    if (arrayOfObject.length <= 0)
      return null;
    return new TreePath(arrayOfObject);
  }

  public static TreePath getTopTreePath(JTree paramJTree, TreePath[] paramArrayOfTreePath)
  {
    if ((paramJTree == null) || (paramArrayOfTreePath == null) || (paramArrayOfTreePath.length == 0))
      return null;
    TreePath localTreePath = null;
    int i = 2147483647;
    for (int j = 0; j < paramArrayOfTreePath.length; ++j)
    {
      int k = paramJTree.getRowForPath(paramArrayOfTreePath[j]);
      if (k < i)
      {
        i = k;
        localTreePath = paramArrayOfTreePath[j];
      }
    }
    return localTreePath;
  }

  public static Color getTitleLineBorderColor()
  {
    Border localBorder = UIManager.getBorder("TitledBorder.border");
    if (localBorder instanceof LineBorder)
      return ((LineBorder)localBorder).getLineColor();
    return Color.GRAY;
  }

  public static AbstractButton createMoveUpButton(JList paramJList)
  {
    JButton localJButton = new JButton(BaseUtils.readIcon("/com/fr/design/images/control/up.png"));
    localJButton.addActionListener(new MoveListActionListener(paramJList, true));
    localJButton.setToolTipText(Inter.getLocText("Utils-Move_Up"));
    localJButton.setPreferredSize(new Dimension(24, 24));
    return localJButton;
  }

  public static AbstractButton createMoveDownButton(JList paramJList)
  {
    JButton localJButton = new JButton(BaseUtils.readIcon("/com/fr/design/images/control/down.png"));
    localJButton.addActionListener(new MoveListActionListener(paramJList, false));
    localJButton.setToolTipText(Inter.getLocText("Utils-Move_Down"));
    localJButton.setPreferredSize(new Dimension(24, 24));
    return localJButton;
  }

  public static AbstractButton createMoveUpButton(JTable paramJTable)
  {
    JButton localJButton = new JButton(BaseUtils.readIcon("/com/fr/design/images/control/up.png"));
    localJButton.addActionListener(new MoveListJTableActionListener(paramJTable, true));
    localJButton.setText(Inter.getLocText("Utils-Move_Up"));
    return localJButton;
  }

  public static AbstractButton createMoveDownButton(JTable paramJTable)
  {
    JButton localJButton = new JButton(BaseUtils.readIcon("/com/fr/design/images/control/down.png"));
    localJButton.addActionListener(new MoveListJTableActionListener(paramJTable, false));
    localJButton.setText(Inter.getLocText("Utils-Move_Down"));
    return localJButton;
  }

  public static String popupInputNewName(Window paramWindow, List paramList)
  {
    String str;
    int i;
    int j;
    do
    {
      while (true)
      {
        str = JOptionPane.showInputDialog(paramWindow, Inter.getLocText("Utils-Please_Input_a_New_Name") + ":", "");
        if (str == null)
          return null;
        if (str.trim().length() > 0)
          break;
        JOptionPane.showMessageDialog(paramWindow, Inter.getLocText("Utils-New_name_cannot_be_null") + ".", Consts.APP_NAME, 0);
      }
      i = 0;
      if ((paramList != null) && (paramList.size() > 0))
        for (j = 0; j < paramList.size(); ++j)
          if (ComparatorUtils.equals(str, paramList.get(j)))
          {
            JOptionPane.showMessageDialog(paramWindow, "\"" + str + "\" " + Inter.getLocText("Utils-The_Name_has_been_existed") + ".", Consts.APP_NAME, 0);
            i = 1;
            break;
          }
    }
    while (i != 0);
    return str;
  }

  public static String popupRenameName(Window paramWindow, String paramString, List paramList)
  {
    String str;
    int i;
    int j;
    do
    {
      while (true)
      {
        str = JOptionPane.showInputDialog(paramWindow, Inter.getLocText("Rename") + ":", paramString);
        if (str == null)
          return null;
        if (str.trim().length() > 0)
          break;
        JOptionPane.showMessageDialog(paramWindow, Inter.getLocText("Utils-New_name_cannot_be_null") + ".", Consts.APP_NAME, 0);
      }
      i = 0;
      if ((paramList != null) && (paramList.size() > 0))
        for (j = 0; j < paramList.size(); ++j)
          if (ComparatorUtils.equals(str, paramList.get(j)))
          {
            JOptionPane.showMessageDialog(paramWindow, "\"" + str + "\" " + Inter.getLocText("Utils-has_been_existed") + ".", Consts.APP_NAME, 0);
            i = 1;
            str = paramString;
            break;
          }
    }
    while (i != 0);
    return str;
  }

  public static boolean removeJListSelectedNodes(Window paramWindow, JList paramJList)
  {
    int i = paramJList.getSelectedIndex();
    if (i == -1)
      return false;
    int j = JOptionPane.showConfirmDialog(paramWindow, Inter.getLocText("Utils-Are_you_sure_to_remove_the_selected_item") + "?", Inter.getLocText("Remove"), 2, 3);
    if (j == 0)
    {
      int k = paramJList.getMinSelectionIndex();
      int[] arrayOfInt = paramJList.getSelectedIndices();
      Arrays.sort(arrayOfInt);
      for (int l = arrayOfInt.length - 1; l >= 0; --l)
        ((DefaultListModel)paramJList.getModel()).remove(arrayOfInt[l]);
      if (paramJList.getModel().getSize() > 0)
        if (k < paramJList.getModel().getSize())
          paramJList.setSelectedValue(paramJList.getModel().getElementAt(k), true);
        else
          paramJList.setSelectedValue(paramJList.getModel().getElementAt(paramJList.getModel().getSize() - 1), true);
      return true;
    }
    return false;
  }

  public static JFormattedTextField getSpinnerTextField(JSpinner paramJSpinner)
  {
    JComponent localJComponent = paramJSpinner.getEditor();
    if (localJComponent instanceof JSpinner.DefaultEditor)
      return ((JSpinner.DefaultEditor)localJComponent).getTextField();
    System.err.println("Unexpected editor type: " + paramJSpinner.getEditor().getClass() + " isn't a descendant of DefaultEditor");
    return null;
  }

  public static void setColumnForSpinner(JSpinner paramJSpinner, int paramInt)
  {
    JFormattedTextField localJFormattedTextField = getSpinnerTextField(paramJSpinner);
    if (localJFormattedTextField != null)
    {
      localJFormattedTextField.setColumns(paramInt);
      localJFormattedTextField.setHorizontalAlignment(2);
    }
  }

  public static void repaint(Component paramComponent)
  {
    SwingUtilities.invokeLater(new Runnable(paramComponent)
    {
      private final Component val$component;

      public void run()
      {
        this.val$component.invalidate();
        this.val$component.validate();
        this.val$component.repaint();
      }
    });
  }

  static class MoveListJTableActionListener
    implements ActionListener
  {
    private JTable nameTable = null;
    private boolean isUp = true;

    public MoveListJTableActionListener(JTable paramJTable, boolean paramBoolean)
    {
      this.nameTable = paramJTable;
      this.isUp = paramBoolean;
    }

    public void actionPerformed(ActionEvent paramActionEvent)
    {
      TableModel localTableModel;
      ListTableModel localListTableModel;
      List localList;
      Object localObject;
      int i = this.nameTable.getSelectedRow();
      if (i == -1)
        return;
      if (this.isUp)
      {
        if (i > 0)
        {
          localTableModel = this.nameTable.getModel();
          if (localTableModel instanceof ListTableModel)
          {
            localListTableModel = (ListTableModel)localTableModel;
            localList = localListTableModel.getList();
            localObject = localList.get(i - 1);
            localList.set(i - 1, localList.get(i));
            localList.set(i, localObject);
            localListTableModel.fireTableDataChanged();
            this.nameTable.setRowSelectionInterval(i - 1, i - 1);
          }
        }
      }
      else
      {
        localTableModel = this.nameTable.getModel();
        if ((i < localTableModel.getRowCount() - 1) && (localTableModel instanceof ListTableModel))
        {
          localListTableModel = (ListTableModel)localTableModel;
          localList = localListTableModel.getList();
          localObject = localList.get(i + 1);
          localList.set(i + 1, localList.get(i));
          localList.set(i, localObject);
          localListTableModel.fireTableDataChanged();
          this.nameTable.setRowSelectionInterval(i + 1, i + 1);
        }
      }
    }
  }

  static class MoveListActionListener
    implements ActionListener
  {
    private JList nameObjectList = null;
    private boolean isUp = true;

    public MoveListActionListener(JList paramJList, boolean paramBoolean)
    {
      this.nameObjectList = paramJList;
      this.isUp = paramBoolean;
    }

    public void actionPerformed(ActionEvent paramActionEvent)
    {
      DefaultListModel localDefaultListModel;
      Object localObject;
      int i = this.nameObjectList.getSelectedIndex();
      if (i == -1)
        return;
      if (this.isUp)
      {
        if (i > 0)
        {
          localDefaultListModel = (DefaultListModel)this.nameObjectList.getModel();
          localObject = localDefaultListModel.get(i - 1);
          localDefaultListModel.set(i - 1, localDefaultListModel.get(i));
          localDefaultListModel.set(i, localObject);
          this.nameObjectList.setSelectedIndex(i - 1);
          this.nameObjectList.ensureIndexIsVisible(i - 1);
        }
      }
      else
      {
        if (i == -1)
          return;
        if (i < this.nameObjectList.getModel().getSize() - 1)
        {
          localDefaultListModel = (DefaultListModel)this.nameObjectList.getModel();
          localObject = localDefaultListModel.get(i + 1);
          localDefaultListModel.set(i + 1, localDefaultListModel.get(i));
          localDefaultListModel.set(i, localObject);
          this.nameObjectList.setSelectedIndex(i + 1);
          this.nameObjectList.ensureIndexIsVisible(i + 1);
        }
      }
    }
  }

  static class RenameActionListener
    implements ActionListener
  {
    private JList nameList;

    public RenameActionListener(JList paramJList)
    {
      this.nameList = paramJList;
    }

    public void actionPerformed(ActionEvent paramActionEvent)
    {
      int i = this.nameList.getSelectedIndex();
      if (i == -1)
        return;
      Window localWindow = SwingUtilities.getWindowAncestor(this.nameList);
      ArrayList localArrayList = new ArrayList();
      DefaultListModel localDefaultListModel = (DefaultListModel)this.nameList.getModel();
      for (int j = 0; j < localDefaultListModel.size(); ++j)
      {
        localObject = (Nameable)localDefaultListModel.get(j);
        localArrayList.add(((Nameable)localObject).getName());
      }
      Nameable localNameable = (Nameable)this.nameList.getSelectedValue();
      Object localObject = GUICoreUtils.popupRenameName(localWindow, localNameable.getName(), localArrayList);
      if (localObject != null)
      {
        localNameable.setName((String)localObject);
        this.nameList.repaint();
      }
    }
  }
}